﻿using PesticideManage.API.Models;
using PesticideManage.Domain.AggregatesModel.BatchQRCodeAggregate;
using Shang.COSXML;
using System.Text;

namespace PesticideManage.API.Services.CosXml;
public interface IFileService
{

}
public class CosFileService : IFileService
{
    private readonly CosXmlService _cos;

    public CosFileService(CosXmlService cos)
    {
        this._cos = cos;
    }
    public void CreateFile(string fileName, StringBuilder sb)
    {
        char[] dest = new char[sb.Length];
        sb.CopyTo(0, dest, 0, sb.Length);
        byte[] bytes = Encoding.UTF8.GetBytes(dest);
        _cos.PutFileAsync($"{Config.FilePath}/{fileName}", bytes);
    }
    public void CreateFile(QRCodeNumerDTO qrcodeDTO)
    {
        string strNumer = qrcodeDTO.ToString();
        string pesticideId = qrcodeDTO.PesticideId.ToString().PadLeft(4, '0');
        string url = $"{qrcodeDTO.Host}{strNumer}{pesticideId}{DateTime.Now:yyMMddHHmm}";
        StringBuilder sb = new();
        for (int i = 1; i <= qrcodeDTO.Count; i++)
        {
            string oneLine = $"{url}{i.ToString().PadLeft(7, '0')}";
            sb.AppendLine(oneLine);
        }
        char[] dest = new char[sb.Length];
        sb.CopyTo(0, dest, 0, sb.Length);
        byte[] bytes = Encoding.UTF8.GetBytes(dest);
        _cos.PutFileAsync($"{Config.FilePath}/{qrcodeDTO.FileName}", bytes);
    }
}
